import {getMemberLevelList,deleteMemberLevel,updateMemberLevel} from '@/api/Member/index.js'
import { useConfirm } from '@/hooks/index.js'

const seachTableRef = ref(null)
export const useCommonData = (props) => {
  return {
    seachTableRef
  }
}
export const useCurd = (props) => {
  // 表格数据
  const tableLoading = ref(false)
  const tableData = ref([])

  // 搜索
  const handlerSearch = async (data) => {
    try{
      tableLoading.value = true
      tableData.value = await getMemberLevelList(data)
    }finally{
      tableLoading.value = false
    }
  }
  // 状态变化
  const handlerState = async (val,row) => {
    ElMessageBox.confirm(`确定${val?'启用':'禁用'}会员等级【${row.levelName}】?`, '提示', {
      type: 'warning',
      beforeClose: async (action, instance, done) => {
        const args = [row.id, {levelName:row.levelName,levelIntegralNum:row.levelIntegralNum,levelStatus:val?1:0 }]
        const res = await useConfirm(action, instance, done,updateMemberLevel,...args)
        if(res){
          row.levelStatus = val
          seachTableRef.value.handlerSearch()
          ElMessage.success(`${val?'启用':'禁用'}成功`)
        }
      }
    })
  }

  /** 弹窗相关 */
  const editDialogShow = ref(false)
  const itemDetail = ref({})
  const openEditDialog = (row={}) => {
    itemDetail.value = row
    editDialogShow.value = true
  }
  const closeEditDialog = () => {
    itemDetail.value = {}
    editDialogShow.value = false
  }
  /** 弹窗相关end */

  // 表格配置
  const btnLefts = [
    {
      label:'新增',
      type:'primary',
      tips:'*会员等级最多可创建10个，默认会员不支持删除',
      tipsStyle:{
        color:'#666',
        fontSize:'12px',
        margin:'0 12px'
      },
      show:()=>true,
      img:new URL("@/assets/icon/btn-add.png",import.meta.url),
      click:()=>{
        if(tableData.value.length>=10){
          ElMessage.error('会员等级最多可创建10个！')
          return
        }
        openEditDialog()
      }
    },
  ]
  const btnRights = []
  const tableConfig = ref([
    {
      prop:'levelName',
      label:'等级名称',
    },
    {
      prop:'levelIntegralNum',
      label:'获取条件',
      format:(value)=>`累计积分达到${value}分,升级为此等级`
    },
    {
      prop:'userTotal',
      label:'已有人数',
      type:'slot',
      slotName:'userTotal'
    },
    {
      type:'slot',
      prop:'levelStatus',
      label:'状态',
      width:120,
      slotName:'switch'
    },
    {
      type: "action",
      label: "操作",
      fixed: "right",
      btns:[
        {
          label:()=>'编辑',
          type:"success",
          show:(row)=>true,
          click:(row)=>{
            openEditDialog(row)
          }
        },
        {
          label:()=>'删除',
          type:"danger",
          show:(row)=>row.levelType,
          click:(row)=>{
            console.log('详情',row)
            ElMessageBox.confirm(`确定删除会员等级【${row.levelName}】?`, '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'error',
              beforeClose: async (action, instance, done) => {
                const res = await useConfirm(action, instance, done,deleteMemberLevel,row.id)
                if(res){
                  seachTableRef.value.handlerSearch()
                  ElMessage.success('删除成功')
                }
              }
            })
          }
        }
      ]
    }
  ])
  const tableForms = ref([])
  return {
    seachTableRef,
    btnLefts,
    btnRights,
    tableConfig,
    tableForms,
    tableLoading,
    tableData,
    handlerSearch,
    handlerState,
    editDialogShow,
    itemDetail,
    closeEditDialog
  }
}